package com.ruoyi.project.syl_LiBiao.pay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

@Controller
public class AliPayController {
    // 应用标识
    private final String APP_ID = "2016092800619225";
    // 应用私钥
    private final String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCqB1ogYkG1Q/VEEp7g14V0btpu7rFMwdjVQnyC1auaP5AlIAlBEpMbkEA3K/oBK81GRMMWelSDuBz3NXcS1H7zNYoMs1DvaboeVGAJEzh9PIOxppWMS2fr7/xkWTM/KP8eV+pTdVqG5YygdBs7FF/hKaNGNYJrb3KWHGUG9pCVp6zizLi2vbmx0y2YtjUYzivJHncVSB+lU2g55Par6G/YZ5wTa1zy2iVKhJY04WM+U+0WfAlMuW0x7U1o1d2ax9rAVfIeIK40lscFh9nXWRDNHKHVRr0dziFRR8Kqho58l3m69reWF4yPYLpw1fmGcEDu7Wm5p/6vmLztGPP1Ggb1AgMBAAECggEAGnUmax+fBevbQIp9OpOO6r1NNDHkWQNZEGZWJa3GFCyL65r+laekSIe71ln7fy8ntCWDDf5fJFgD+dvR+ZYjYwkTwEIu9/JFye5rOz5IbgR6oaxAXSJmu2oOpjbTKKLigABCMFZ0jcUuitFGY30aJllqVMSC1OD0vbMRbXH0sbSyaXheUdcXsPI5e0zZ8GNA8DjsJNrnap1LiAbZe749PkYuXQNy4OLKfBM9teIawT9Z5XWxYie8ZxOi59mWYYQ8ttwkr0nFLSJMwGpga7Mblt0u73oqs20Ve5fB7vNXoBsdnIWoBVhe9ewpKeEJatCXfvw0caWuH/2p5u5VumGSwQKBgQDq37eO2qwErAhndJKQ5FmJ+OAowH7y+2eXUW1I+uIaao5Ypeht3pjs6xKSETS1sWoEJ+2GAptr1PWDhv9kfM+lX8VqqreIEXbTQ4YHGV6Jd9mEHLtceUJMaEEICl2NK3RK3cZ64ExupMcoVBGxpG904WaHeJqgCcpNAFY0C+mqHQKBgQC5Un0lL2z+Raf4dZ11yJVVQzfrrqDNVbU9EZOa8yckxER8u6L3znoRsUm0yShrZTK632eh3mW8T0K/Gc4VOtKMW0V7kPh5qDvemmqh1vFb2v3JaJbFEJhCiwJ5MIU4DiTlrEGLaGqQLX8I5gmITP3gmQGm0GMTstCmId9mk5r4uQKBgHL1bvWHyDw9K5vfbcFnUv/6BVVM6pXpQUnJ/BCFY1eEogl3ouzrz7PeVueRblgj7vz7wgEek43S3Rw0XhqEEGokFw9fYZdC3QMIgLhGzYPBWoCUQ4w+eZnGSVYzJJjtFO1z4SrRT/ezxRrTExn8nuT5UeKqwBvmztKjgI3W6tZtAoGBAIlkUWlfFrQQWi5GrD0lPykeE3hEV+zDLQGgZUkMGaoVZB0BUxna1v8E6/r+41tzhD+s3vzPj6cjL/3TmMh+epgaFpI8cSpogjJPUty3KOsLmPlX8+cXnXo/6s6EHU/olIw9v74l5FMNl8+2E70cL1/yr5nHhpyVBkEUfbZjmhHpAoGACMECYjpBuK8q8GKAbY5mUVMce7+IT+24+OAdJuywc/IJA70ELtQxI7oHX8JWwhg6EQcBI9xYHY90kPeVl5QgeEbDXYRM0VGf3qm1wyJ9uyRR6bqhKKvZLhv9OgIvJnkoki/dnFmM47w8NPreqWpJb49JUZ/YM9NiZwQ8c0DOrUM=";
    // 编码格式
    private final String CHARSET = "UTF-8";
    // 支付宝公钥
    private final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhDzOnKZEUTuWBnx9dCVfJo7ET7VMDDynzz6gD8VmIkcT2R5AJifEXeUCmkoOeb3SgZ7MuKCZtu5oUpWXpe3ykAjtuBnYdiLEnMyCuX4n59yIa50ohEQ1XR+Z7VtzsXWh/Kb/9RxbOq89t13vkD7Qsn7dhKQMz0ZNdM6teIxQeLaLhQiky0iHAyr9UC3QJvxpFsW7yl03EVZ/p3LnEVLR0v723QHNwx5mUdKf40BH/D5k/n3h0rasnwFNTveit5aLVP3GFdEy9hHLeCK8+uZnW676gETDApFEV55NppgQvqaY7QyrNZWzYcsZz/cJokJE3Rla9uDrVD6PMWMpbQ+z+wIDAQAB";
    // 沙箱路径接口，正是路径应为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL = "https://openapi.alipaydev.com/gateway.do";
    // 参数返回格式
    private final String FORMAT = "JSON";
    // 签名方式
    private final String SIGN_TYPE = "RSA2";
    // 支付宝异步通知路径，付款完毕后会异步调用本项目的方法，必须为公网地址
    private final String NOTIFY_URL = "http://127.0.0.1/notifyURL";
    // 支付宝同步通知路径，也就是当付款完毕后跳转本项目的页面，可以不是公网地址
    private final String RETURN_URL = "http://localhost:8080/sylCharge/pay";

    @RequestMapping("/alipay")
    @ResponseBody
    public void alipay(HttpServletResponse response) throws IOException {
        System.out.println("进入支付宝");
        // 实例化客户端,填入所需参数
        // 网关地址 应用标识 应用私钥 传输格式 编码格式 支付宝公钥 签名类型
        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //在公共参数中设置回跳和通知地址
        request.setReturnUrl(RETURN_URL);
        //request.setNotifyUrl(NOTIFY_URL);
        //商户订单号，商户网站订单系统中唯一订单号，必填
        //生成随机Id
        String out_trade_no = UUID.randomUUID().toString();
        //付款金额，必填
        String total_amount =Integer.toString(100);
        //订单名称，必填
        String subject ="处方收费";
        //商品描述，可空
        String body = "您的药品费用";
        request.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String form = "";
        try {
            form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        response.setContentType("text/html;charset=" + CHARSET);
        response.getWriter().write(form);// 直接将完整的表单html输出到页面
        response.getWriter().flush();
        response.getWriter().close();
    }
}


